package cn.cvs.web.interceptor;

import cn.cvs.pojo.SysUser;
import cn.cvs.utils.Constants;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义拦截器
 */
public class SysInterceptor extends HandlerInterceptorAdapter {
	private Logger logger = Logger.getLogger(SysInterceptor.class);

	/**
	 * 前置处理方法
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 */
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
		logger.debug("进入自定义拦截器==>SysInterceptor");
		HttpSession session = request.getSession();
		// 获取登录用户
		SysUser sysUser = (SysUser)session.getAttribute(Constants.USER_SESSION);
		// 验证登录状态
		if(null == sysUser){
			response.sendRedirect(request.getContextPath()+"/401.jsp");
			return false;
		}
		return true;
	}
}
